//
//  AccordionNavigator.m
//  Ghac 4S
//
//  Created by apple on 11-10-19.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "AccordionNavigator.h"
#import "AccordionTitleView.h"

@interface AccordionNavigator(private)
-(void)setAllFrames:(NSInteger)selectedIndex;
-(void)setCurrentPage:(NSInteger)currentPage animate:(BOOL)willAnimate;
-(void)titleBtnTouch:(id)sender;
@end

@implementation AccordionNavigator
@synthesize currentPage = m_selectedIndex;
@synthesize frameSize = m_titleFrameSize;

-(id) initWithFrame:(CGRect)frame
{
    CGSize size = CGSizeMake(frame.size.width, 30);
    return [self initWithFrame:frame titleFrameSize:size];
}

- (id)initWithFrame:(CGRect)frame titleFrameSize:(CGSize)titleFrameSize
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // Initialization code
        m_titleFrameSize = titleFrameSize;
        m_spacer = 0.0f;
        [m_pageViewList release];
        [m_titleViewList release];
        [m_scrollViewList release];
        m_pageViewList = [[NSMutableArray alloc] init];
        m_titleViewList = [[NSMutableArray alloc] init];
        m_scrollViewList = [[NSMutableArray alloc] init];
        m_selectedIndex = 0;

    }
    return self;
}
-(void)dealloc
{
    [m_pageViewList release];
    [m_titleViewList release];
    [m_scrollViewList release];
    [super dealloc];
}
-(NSUInteger)pageViewCount
{
    return m_pageViewList.count;
}
-(void)addPageView:(UIView *)view withTitleView:(AccordionTitleView *)titleView
{
    //add pages
    [m_pageViewList addObject:view];
    [m_titleViewList addObject:titleView];
    
    UIScrollView* scrollView = [[UIScrollView alloc] init];
    [m_scrollViewList addObject:scrollView];
    [scrollView setDelegate:self];
    
    
    [scrollView addSubview:view];
    [self addSubview:scrollView];
    [self addSubview:titleView];
    [self setAllFrames:m_selectedIndex];
    [titleView addTarget:self action:@selector(titleBtnTouch:) forControlEvents:UIControlEventTouchUpInside];
    
    [scrollView release];
}
-(void)titleBtnTouch:(id)sender
{
    NSInteger index = [m_titleViewList indexOfObject:sender];
    [self setCurrentPage:index animate:YES];
}
-(void)setCurrentPage:(NSInteger)currentPage animate:(BOOL)willAnimate
{
    if (m_selectedIndex == currentPage) return;
    
    
    m_selectedIndex = currentPage;
    
    if (willAnimate)
    {
        CGContextRef context = UIGraphicsGetCurrentContext();
        [UIView beginAnimations:@"animate" context:context];
        [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self cache:YES];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
        [UIView setAnimationDuration:0.5];
        [self setAllFrames:currentPage];
        [UIView commitAnimations];
    }
    else
    {
        [self setAllFrames:currentPage];
    }
    
    
}
-(void)setAllFrames:(NSInteger)selectedIndex
{
//    if (selectedIndex == m_selectedIndex) return;
    
    CGFloat tmpY = 0;
    for (NSInteger i = 0; i < m_titleViewList.count; i++)
    {
        
        AccordionTitleView* titleView = [m_titleViewList objectAtIndex:i];
        UIScrollView* scrollView = [m_scrollViewList objectAtIndex:i];
        UIView* pageView = [m_pageViewList objectAtIndex:i];
        [titleView setFrame:CGRectMake(0, tmpY, m_titleFrameSize.width, m_titleFrameSize.height)];
        tmpY += m_titleFrameSize.height + m_spacer;
        
        if (i == selectedIndex)
        {
            
            CGSize size = CGSizeMake(m_titleFrameSize.width, self.frame.size.height - [self pageViewCount] * (m_titleFrameSize.height + m_spacer));
            [scrollView setFrame:CGRectMake(0, tmpY, size.width, size.height)];
            CGSize contentSize = CGSizeMake(pageView.frame.origin.x + pageView.frame.size.width, pageView.frame.origin.y + pageView.frame.size.height);
            [scrollView setContentSize:contentSize];
            scrollView.alwaysBounceHorizontal = NO;
            [titleView setSelected:YES];
            tmpY += scrollView.frame.size.height + m_spacer;
        }
        else
        {
            [titleView setSelected:NO];
            [scrollView setFrame:CGRectMake(0, tmpY, m_titleFrameSize.width, 0)];
        }
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
